//
//  ConfirmTicketViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-27.
//
//

#import "ConfirmTicketViewController.h"
#import "PersonTicketViewController.h"
#import "PassengerViewController.h"

#define BTNTAG 200010

@interface ConfirmTicketViewController ()

@end

@implementation ConfirmTicketViewController
{
    UIScrollView *_myscrollview;
    
    UIButton *_payBtn1;
    UIButton *_payBtn2;
    
    NSInteger _payType;
}

- (void)dealloc
{
    [_myscrollview release];
    [_payBtn1 release];
    [_payBtn2 release];
    
    _myscrollview = nil;
    _payBtn1 = nil;
    _payBtn2 = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    _userArray = self.userArray;
    
    //初始化视图
    self.title = @"机票";
    //加载背影
     [self.view setBackgroundColor:BackgroundColor];
    
    //实始化视图
    [self init_scrollview];
}

//实始化视图
- (void)init_scrollview
{
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    _myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)];
    _myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    _myscrollview.pagingEnabled = NO; //是否翻页
    _myscrollview.backgroundColor = [UIColor whiteColor];
    _myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    _myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    _myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    _myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(width, 1000 + [_userArray count]*100);
    [_myscrollview setContentSize:newSize];
    
    //初始化视图
    [self init_view:_myscrollview];
    
    [self.view addSubview:_myscrollview];
}

//初始化视图
- (void)init_view:(UIScrollView*)myscrollview
{
    //初始化票务
    for (int i  = 0; i < [_userArray count]; i ++) {
        NSDictionary *useDict = [_userArray objectAtIndex:i];
        
        UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        bgBtn.frame = CGRectMake(10, 20+i*90, 300, 80);
        [bgBtn setBackgroundImage:[UIImage imageNamed:@"flightticket1_03"] forState:UIControlStateNormal];
        [myscrollview addSubview:bgBtn];
        
        UIButton *delBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        delBtn.frame = CGRectMake(30, 50+i*90, 20, 20);
        delBtn.tag = BTNTAG + i;
        [delBtn addTarget:self action:@selector(delEvent:) forControlEvents:UIControlEventTouchUpInside];
        [delBtn setBackgroundImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
        [myscrollview addSubview:delBtn];
        
        UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(60, 30+90*i, 200, 30)]autorelease];
        namelbl.backgroundColor = [UIColor clearColor];
        namelbl.text = [NSString stringWithFormat:@"姓名：%@", [useDict objectForKey:@"name"]];
        namelbl.font = [UIFont systemFontOfSize:20];
        [myscrollview addSubview:namelbl];
        
        UILabel *idnumlbl = [[[UILabel alloc]initWithFrame:CGRectMake(60, 60+90*i, 200, 30)]autorelease];
        idnumlbl.backgroundColor = [UIColor clearColor];
        idnumlbl.text = [NSString stringWithFormat:@"身份证：%@", [useDict objectForKey:@"idnum"]];
        idnumlbl.font = [UIFont systemFontOfSize:15];
        [myscrollview addSubview:idnumlbl];
    }
    
    //初始化支付视图
    [self initPayView:myscrollview];
}


//初始化支付视图
- (void)initPayView:(UIScrollView*)myscrollview
{
    NSInteger index = [_userArray count];
    UIButton *addTicketBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    addTicketBtn.frame = CGRectMake(20, 20+index*90, 280, 40);
    [addTicketBtn setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [addTicketBtn setTitle:@"继续添加乘机人" forState:UIControlStateNormal];
    [myscrollview addSubview:addTicketBtn];
    [addTicketBtn addTarget:self action:@selector(addticketEvent) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *paylbl = [[[UILabel alloc]initWithFrame:CGRectMake(30, 70+index*90, 180, 30)]autorelease];
    paylbl.text = @"选择支付方式:";
    paylbl.backgroundColor = [UIColor clearColor];
    [myscrollview addSubview:paylbl];
    
//    _payBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(30, 110+index*90, 100, 30)];
//    [_payBtn1 setBackgroundImage:[UIImage imageNamed:@"zfb.png"] forState:UIControlStateNormal];
//    [_payBtn1 setBackgroundImage:[UIImage imageNamed:@"zhifubao2.png"] forState:UIControlStateSelected];
//    [_payBtn1 addTarget:self action:@selector(pay1ButtonPick:) forControlEvents:UIControlEventTouchDown];
//    [myscrollview addSubview:_payBtn1];
    
    _payBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(30, 110+index*90, 100, 30)];
    [_payBtn2 setBackgroundImage:[UIImage imageNamed:@"yinlian1"] forState:UIControlStateNormal];
    [_payBtn2 setBackgroundImage:[UIImage imageNamed:@"yinlian2"] forState:UIControlStateSelected];
    [_payBtn2 addTarget:self action:@selector(pay2ButtonPick:) forControlEvents:UIControlEventTouchDown];
    [myscrollview addSubview:_payBtn2];
    
    
//    _payBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(30, 110+index*90, 100, 30)];
//    //[payBtn1 setBackgroundImage:[UIImage imageNamed:@"zfb"] forState:UIControlStateNormal];
//    [_payBtn1 setBackgroundImage:[UIImage imageNamed:@"zhifubao2"] forState:UIControlStateNormal];
//    //[payBtn1 addTarget:self action:@selector(pay1ButtonPick:) forControlEvents:UIControlEventTouchDown];
//    [myscrollview addSubview:_payBtn1];
//    
//    _payBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(200, 110+index*90, 100, 30)];
//    //[_payBtn2 setBackgroundImage:[UIImage imageNamed:@"yinlian1"] forState:UIControlStateNormal];
//    [_payBtn2 setBackgroundImage:[UIImage imageNamed:@"yinlian2"] forState:UIControlStateNormal];
//    //[_payBtn2 addTarget:self action:@selector(pay2ButtonPick:) forControlEvents:UIControlEventTouchDown];
//    [myscrollview addSubview:_payBtn2];
    
    
    UIButton *nextStepBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStepBtn.frame = CGRectMake(20, 150+index*90, 280, 40);
    [nextStepBtn setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [myscrollview addSubview:nextStepBtn];
    
    [nextStepBtn addTarget:self action:@selector(nextEvent) forControlEvents:UIControlEventTouchUpInside];
}

-(void)pay1ButtonPick:(id)sender
{
    [_payBtn1 setSelected:YES];
    [_payBtn2 setSelected:NO];
    _payType = 0;
}

-(void)pay2ButtonPick:(id)sender
{
    [_payBtn1 setSelected:NO];
    [_payBtn2 setSelected:YES];
    _payType = 1;
}



//确认支付
- (void)nextEvent
{
    PersonTicketViewController *personController = [[PersonTicketViewController alloc]init];
    personController.userArray = self.userArray;
    personController.orderInfo = self.orderInfo;
    personController.startCity = self.startCity;
    personController.arriveCity = self.arriveCity;
    personController.startDate = self.startDate;
    [self.navigationController pushViewController:personController animated:YES];
    [personController release];
}

//继续添加机票
- (void)addticketEvent
{
    PassengerViewController *passController = [[PassengerViewController alloc]init];
    passController.orderInfo = self.orderInfo;
    passController.userArray = self.userArray;
    passController.startCity = self.startCity;
    passController.arriveCity = self.arriveCity;
    passController.startDate = self.startDate;
    [self.navigationController pushViewController:passController animated:YES];
    [passController release];
}

//删除机票
- (void)delEvent:(id)sender
{
    UIButton *button = (UIButton*)sender;
    NSInteger index = button.tag % BTNTAG;
    [_userArray removeObjectAtIndex:index];
    
    [_myscrollview release];
    _myscrollview = nil;
    [self init_scrollview];
}

@end

